import React, {Component} from 'react'
import {View, Text, TextInput} from 'react-native'
import AttachmentManager from '../common/attachment_manager'
import AudioSelector from '../common/audio_selector'
import ImageSelector from '../common/image_selector'
import VideoSelector from '../common/video_selector'
import {Aws3UploadApi} from '../../config/api'

//作业编辑
class AnswerOperate extends Component {
    static defaultProps = {
        onFinishedUpload: () => {},
        onChangeContent: () => {},
        attachments: [],
        content: ''
    }

    _get_name = () => {
        let date = new Date()
        let name = date.getTime().toString()
        name = name.substr(name.length - 6, 6)
        return name
    }

    finishedAudioRecord = (filePath) => {
       let name = this._get_name()
        Aws3UploadApi(`${name}.aac`, filePath, 'audio/aac').then((result) => {
           this.props.onFinishedUpload(name, 'audio', result.awsPath)
        })
    }

    finishedImageSelect = (filePath) => {
        let name = this._get_name()
        Aws3UploadApi(`${name}.jpeg`, filePath, 'image/jpeg').then((result) => {
            this.props.onFinishedUpload(name, 'image', result.awsPath)
        })
    }

    finishedVideoSelect = (filePath) => {
        let name = this._get_name()
        Aws3UploadApi(`${name}.mp4`, filePath, 'video/mp4').then((result) => {
            this.props.onFinishedUpload(name, 'video', result.awsPath)
        })
    }

    render () {
        return (
            <View>
                 <TextInput
                    placeholder={'请输入内容'}
                    onChangeText={(txt) => {this.props.onChangeContent(txt)}}
                    style={{height: 150, padding: 0}}
                    multiline={true}
                    textAlignVertical={'top'}
                    underlineColorAndroid="transparent"
                    blurOnSubmit={true}
                    value={this.props.content}
                />
                <View style={{flexDirection: 'row'}}>
                    <ImageSelector style={{flex: 1}} onFinished={(filePath) => {this.finishedImageSelect(filePath)}} />
                    <AudioSelector style={{flex: 1}} onFinished={(filePath) => {this.finishedAudioRecord(filePath)}} />
                    <VideoSelector style={{flex: 1}} onFinished={(filePath) => {this.finishedVideoSelect(filePath)}}/>
                </View>
                <AttachmentManager attachments={this.props.attachments} />
            </View>
        )
    }
}


export default AnswerOperate